<?php
/**
 * static
 *
 * script
 *
 * @version:	0.1a
 * @package:	global
 * @author:	alex_ez
 * @created:	27.01.2009 01:52:13
 *
 * @copyright:	(c) 2009 alex <qfox@ya.ru> yaroshevich aka alex_ez. All rights reserved.
 */


class static_controller extends controller
{
	protected $quantity;
	
	public function on_start( )
	{
		$this ->model = new staticdata( );
		$this ->quantity = config::instance( ) ->news__quantity;
	}
	
	public function page_index( $p, $o )
	{
		$tag = isset($p['tag']) ? $p['tag'] : ( isset($o['tag']) ? $o['tag'] : 'front' );
		if( $tag != 'front' )
			$tag = texo::latinate( $tag, '/' );
		
		$v = $this ->model ->by_tag( trim( $tag, '/' ), 0 );
		if( is_null( $v ) )
			response::instance( ) ->notfound( );
			//return 'page not found.';
		
		$meta = (array)(json::decode( $v['meta'] ));
		$meta += array( 'title' => $v['title'] );
		$meta += (array)(isset($o['meta'])?$o['meta']:array());
		$this->meta = $meta;

		if( $v['title'] && $v['tag'] != 'index' ) {
			$this->crumbs += $this->get_crumbs_for_tag($v['tag']);
		}

		if( isset($p['return']) && $p['return'] )
			return $v;

		return sprintf( '<div class="static-body static-%1$s-container-body">%2$s</div>', str_replace( array( '/', '_', '.' ), '-', $tag ), $v['body'] );
	}
	
	public function page_all( $p, $o )
	{
		$page = (isset($p['page'])) ? (int)$p['page'] : 1;
		
		$list = $this ->model ->by( '`tag` like "articles/%"', 'title-', array( ($page-1)*$this ->quantity, $this ->quantity) );
		$found_rows = $this ->model ->get_found_rows( );
		$total_pages = ( intval( $found_rows / $this ->quantity) ) + (( $found_rows % $this ->quantity)>0 ? 1:0 );
		
		$r = array( 'list' => $list, 'total_pages' => $total_pages, 'current_page' => $page );
		
		return $r;
	}

	public function get_crumbs_for_tag( $tag ) {
		$tag = explode('/',$tag);
		$tag0 = '';
		$tags = array();
		foreach ($tag as $tagpart) {
			$tag0 .= '/'.$tagpart;
			$tags[] = trim($tag0,'/');
		}

		$list = $this->model->by( array( 'tag' => $tags ), 'length(`tag`)+,`title`+' );

		$r = array();
		foreach ($list as $i)
			$r[site_url($i['tag'],1)] = $i['title'];
		return $r;
	}

	public function block_index( $p ) {
		if (is_string($p)) {
			parse_str($p, $o);
			if (!is_array($o))
				return '';
			$p = $o;
		}
		return $this->text( $p );
	}
	
	public function block_text( $p ) {
		$debug = isset($p['debug']);
		
		if( !$p['tag'] ) return $debug ? '<em class="error">no tag param!</em>' : '';
		
		$tag = ( @$p['strict'] ) ? $p['tag'] : '$'.$p['tag'];
		$static = $this ->model ->by_tag( $tag );
		if( !$static ) return $debug ? '<em class="error">no record with tag '.$tag.'</em>' : '';
		
		// current controller
		$cc = self::instance_of( self::get_page_controller().'_controller' );
		
		if( !@$p['nometa'] )
		{
		if( $static['meta'] )
			$static['meta'] = json::decode( $static['meta'] );
		
		// replace current metas by block metas
		$meta = $cc ->meta;
		if( !is_null( $meta ) )
		foreach( array( 'title', 'keywords', 'description' ) as $kk )
		{
			if( !empty( $static['meta'][$kk] ) && !is_null( $static['meta'][$kk] ) )
				$meta[$kk] = $static['meta'][$kk];
		}
		$cc ->meta = $meta;
		}
		
		$r = $static['body'];
		
		if( @$p['title'] && $static['title'] )
		{
			$t = ($p['title'][0]=='h'&&$p['title'][1]>0) ? substr($p['title'],0,2) : 'h3';
			$r = sprintf( '<%1$s%3$s>%2$s</%1$s>%4$s', $t, $static['title'], (@$p['anchor']?sprintf(' id="%s"',$p['anchor']):''), $r );
		}
		
		return $r;
	}
	
	public function exists( $tag, $type = 'page' )
	{
		if( strpos( 'page,block', $type ) === false ) $type == 'page';
		$prefix = ( $type == 'page' ) ? '' : '$';
		
		return !!( $this ->model ->count( array( 'tag' => $prefix.$tag ) ) );
	}

	public function get_sitemap()
	{
		$list = $this->model->by( '`tag` not like "$%" && `tag` <> "index" ', 'tag+' );

		$r = array();
		foreach ($list as $i)
			$r[site_url($i['tag'],1)] = array(
				'href' => site_url($i['tag']),
				'text' => $i['title'],
				'tm' => $i['tm'],
				'changefreq' => 'monthly',
				'priority' => '0.6',
				);
		return $r;
	}

}

